package setup;

import interceptor.InjectConnection;

import java.sql.Connection;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

import question.ClassifyInfo;
import question.Classify;
import question.QuestionVar;
import utils.Var;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import account.AccountUtils;

/**
 * Created by IntelliJ IDEA.
 * User: Shawn
 * Date: 2009-6-18
 * Time: 23:21:51
 */
public class QueryAction {

  private Connection con;

  @InjectConnection
  public void setCon(Connection con) {
    this.con = con;
  }

  private List<ClassifyInfo> kmList;

  public String queryKm() throws Exception {
    HttpSession session = ServletActionContext.getRequest().getSession();
    if (!AccountUtils.hasRight(session, Var.RIGHT_SETUP)) throw new Exception("deny!");

    kmList = Classify.getClassifyList(con, QuestionVar.CLASSIFY_TYPE_KM);
    return Var.ACTION_SUCCESS;
  }

  public List<ClassifyInfo> getKmList() {
    return kmList;
  }

  private int kmID;
  private List<ClassifyInfo> dtList;
  private Map<Integer, List<ClassifyInfo>> ydMap;
  private Map<Integer, SetupInfo> setupMap;

  public String queryKmDetail() throws Exception {
    HttpSession session = ServletActionContext.getRequest().getSession();
    if (!AccountUtils.hasRight(session, Var.RIGHT_SETUP)) throw new Exception("deny!");

    dtList = Classify.getChildren(con, kmID);

    ydMap = new HashMap<Integer, List<ClassifyInfo>>();
    for (ClassifyInfo info : dtList) {
      List<ClassifyInfo> list = Classify.getChildren(con, info.getCid());
      ydMap.put(info.getCid(), list);
    }

    setupMap = new HashMap<Integer, SetupInfo>();
    List<SetupInfo> setupList = Setup.getSetupList(con, kmID);
    for (SetupInfo info : setupList) setupMap.put(info.getYdID(), info);

    return Var.ACTION_SUCCESS;
  }

  public void setKmID(int kmID) {
    this.kmID = kmID;
  }

  public List<ClassifyInfo> getDtList() {
    return dtList;
  }

  public Map<Integer, List<ClassifyInfo>> getYdMap() {
    return ydMap;
  }

  public Map<Integer, SetupInfo> getSetupMap() {
    return setupMap;
  }
}
